import homeApi from "@/static/js/enterpriseServer/index.js"
import ykAuthpup from "@/components/yk-authpup/yk-authpup";
import choseRegion from '@/components/chose-region1/chose-region.vue'
import wfAddress from "@/components/WF-address/WF-address.vue";
export default {
  data() {
    return {
      name: '',//企业名称
      true_name: '',//联系人
      tel: '',//联系电话
      service_cate_id: '',//服务分类id
      address: '', //地址
      type: '',// 服务商类型 1：官方服务商 2：金牌服务商 3：银牌服务商 4：铜牌服务商 5：一般服务商
      info_images: '',//营业执照
      logo: '',//企业logo
      qual_llicense: '',//资质许可证
      city: '请选择所在地区',//城市
      city_code: '', //城市编码
      typeName: '请选择服务商类型',
      permissionID: '',//调用权限ID
      showChooseImg: false,//是否显示选择图片
      showChooseImgList: [
        {
          name: '拍摄',
          type: 1,
        }, {
          name: '从相册选择',
          type: 2,
        }
      ],//选择图片方式
      activeImgType: -1,//选择图片方式
      imgList1: [],//图片列表
      imgList2: [],//图片列表
      imgList3: [],//图片列表
      activeImgIndex: -1,//选择上传图片的位置

      serverType: [
        // {
        // 	name: '官方服务商',
        // 	id: 1
        // },
        {
          name: '普通服务商',
          id: 5
        }
      ],
      serverCategory: [],//服务类目
      serverCategoryName: '请选择服务类目',
      allCost: {
        "1_margin": "0",
        "1_upkeep": "0",
        "2_margin": "0",
        "2_upkeep": "0",
        "3_margin": "0",
        "3_upkeep": "0",
        "4_margin": "0",
        "4_upkeep": "0",
        "5_margin": "0",
        "5_upkeep": "0",
      },//保证金
      showCityList: false, //是否显示城市列表
    };
  },
  components: {
    choseRegion,
    ykAuthpup,wfAddress
  },
  onLoad() {
    this.getServiceCate()
    this.getDeposit()
  },

  onShow() {
  },
  computed: {
    costNum() {
      if (!this.type) return;
      return (
        Number(this.allCost[this.type + "_margin"]) +
        Number(this.allCost[this.type + "_upkeep"])
      );
    },
  },
  methods: {
    //地址选择
    regionName(e) {
      this.city = e[0].name + '-' + e[1].name
      this.city_code = e[1].id
      console.log(this.city_code, 'this.city_code')
    },

    //图片上传
    ChooseImage() {
      let type = this.activeImgType
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: type == 1 ? ['camera'] : ['album'], //从相册选择
        success: (res) => {
          if (res.tempFilePaths.length > 0) {
            for (let i = 0; i < res.tempFilePaths.length; i++) {
              this.imgUpload(res.tempFilePaths[i]);
              uni.showLoading({
                title: '上传中'
              });
            }
          }
        }
      });
    },

    ChooseImage1(e) {
      this.ChooseImage(e, 'imgList1');
    },

    ChooseImage2(e) {
      this.ChooseImage(e, 'imgList2');
    },

    ChooseImage3(e) {
      this.ChooseImage(e, 'imgList3');
    },


    //获取OSS图片数据
    imgUpload(img) {
      homeApi.uploadImg(img).then(res => {
        let res1 = JSON.parse(res)
        if (res1.code === 1) {
          if (this.activeImgIndex == 1) {
            this.imgList1.push(res1.data.fullurl)
          } else if (this.activeImgIndex == 2) {
            this.imgList2.push(res1.data.fullurl)
          } else if (this.activeImgIndex == 3) {
            this.imgList3.push(res1.data.fullurl)
          }
          uni.hideLoading();
        } else {
          this.$tools.msg(res1.msg);
        }
      })
    },
    previewImage(e, imgListName) {
      uni.previewImage({
        urls: this[imgListName],
        current: e.currentTarget.dataset.url,
      });
    },

    // 调用整合后的函数示例
    viewImage1(e) {
      this.previewImage(e, 'imgList1');
    },

    viewImage2(e) {
      this.previewImage(e, 'imgList2');
    },

    viewImage3(e) {
      this.previewImage(e, 'imgList3');
    },

    DelImg1(e) {
      this.DelImg(e, 'imgList1');
    },

    DelImg2(e) {
      this.DelImg(e, 'imgList2');
    },

    DelImg3(e) {
      this.DelImg(e, 'imgList3');
    },
    //删除图片
    DelImg(e, imgListName) {
      uni.showModal({
        title: '温馨提示',
        content: '确定要删除此图片？',
        cancelText: '再想想',
        confirmText: '确定',
        success: res => {
          if (res.confirm) {
            this[imgListName].splice(e.currentTarget.dataset.index, 1)
          }
        }
      })
    },

    //显示图片方式弹窗
    chooseImgType(e) {
      this.activeImgIndex = e
      this.showChooseImg = true
    },

    //选择图片方式
    selectClick(e) {
      this.activeImgType = e.type
      if (e.type == 1) {
        this.openAuth('CAMERA')
      } else if (e.type == 2) {
        this.openAuth('WRITE_EXTERNAL_STORAGE')
      }
    },

    //打开授权弹窗
    openAuth(permissionID) {
      let sysTemInfo = uni.getSystemInfoSync().brand; //获取手机品牌
      console.log(sysTemInfo, 'sysTemInfo');
      if (sysTemInfo == 'huawei') {
        this.permissionID =
          permissionID; //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
        setTimeout(() => {
          this.$refs['authpup'].open();
        }, 500)
      } else {
        this.ChooseImage()
      }
    },

    //用户授权权限后的回调
    changeAuth() {
      console.log('授权成功');
      //这里是权限通过后执行自己的代码逻辑
      this.ChooseImage()
    },

    cancels(e) {
      console.log(e);
      this.type = '';
      this.typeName = '请选择服务商类型';
    },

    onChange(e) {
      let index = e.detail.value;
      this.type = this.serverType[index].id;
      this.typeName = this.serverType[index].name;
    },
    cancels1(e) {
      console.log(e);
      this.service_cate_id = '';
      this.serverCategoryName = '请选择服务类目';
    },

    onChange1(e) {
      let index = e.detail.value;
      this.service_cate_id = this.serverCategory[index].id;
      this.serverCategoryName = this.serverCategory[index].name;

    },

    //请求服务类目
    getServiceCate() {
      homeApi.getServiceCate().then(res => {
        if (res.code == 1) {
          this.serverCategory = res.data
        } else {
          this.$tools.msg(res.msg);
        }
      })
    },

    //提交
    submit() {
      console.log(this.city_code, 'this.city_code');
      if (this.name == '') {
        this.$tools.msg('请输入企业名称');
        return false;
      }
      if (this.true_name == '') {
        this.$tools.msg('请输入联系人');
        return false;
      }
      if (this.tel == '') {
        this.$tools.msg('请输入联系电话');
        return false;
      }
      if (this.city_code == '') {
        this.$tools.msg('请选择所在地区');
        return false;
      }
      if (this.type == '') {
        this.$tools.msg('请选择服务商类型');
        return false;
      }
      if (this.imgList1.length == 0) {
        this.$tools.msg('请上传营业执照');
        return false;
      }
      if (this.imgList2.length == 0) {
        this.$tools.msg('请上传企业LOGO或门头照');
        return false;
      }
      this.submitServer()
    },

    //提交服务商资料
    submitServer() {
      uni.showLoading({
        title: '提交中'
      });
      homeApi.becomeService({
        name: this.name,
        true_name: this.true_name,
        tel: this.tel,
        city: this.city,
        city_code: this.city_code,
        type: this.type,
        info_images: this.imgList1.join(','),
        logo: this.imgList2.join(','),
        qual_llicense: this.imgList3.join(','),
        service_cate_id: this.service_cate_id,
        token: uni.getStorageSync('token')
      }).then(res => {
        uni.hideLoading();
        if (res.code === 1) {
          if (res.data.is_pay == 1) {
            uni.redirectTo({
              url: `./serverPay?costNum=${this.costNum}&orderId=${res.data.order_id}`
            })
          } else {
            uni.redirectTo({
              url: './serverStarts'
            })
          }
        } else {
          this.$tools.msg(res.msg);
        }
      })

    },

    // 获取服务商保证金和服务维护费
    async getDeposit() {
      const res = await homeApi.getDeposit({});
      if (res.code == 1) {
        this.allCost = res.data;
      }
    },





    showCityListFn() {
      this.showCityList = true;
    },

    closeCityList() {
      this.showCityList = false;
			this.choseRegion1 = null;
			this.city = '请选择所在地区'; //城市
			this.city_code =''; //城市编码
    },

    getCityInfo(data) {
      this.choseRegion1 = data;
			this.cityCode = data.id;
			this.city = data.name; //城市
			this.city_code = data.id; //城市编码
      this.showCityList = false;
    },

  }
}